package com.biscuit.page.basicservice.persistence.dao;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biscuit.page.basicservice.api.model.request.chat.ChatRoomMessagePageAReq;
import com.biscuit.page.basicservice.api.model.request.chat.ChatRoomMessageSaveAReq;
import com.biscuit.page.basicservice.api.model.response.chat.ChatRoomMessageAResp;
import com.biscuit.page.basicservice.model.entities.mysql.SysChatRoomMessage;
import com.biscuit.page.basicservice.persistence.mapper.ChatRoomMessageMapper;
import com.biscuit.page.basicservice.utils.WrapperUtil;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author biscuit
 * @createTime 2024/6/11 10:07
 */
@Component
public class ChatRoomMessageDao extends ServiceImpl<ChatRoomMessageMapper, SysChatRoomMessage> {

    public List<ChatRoomMessageAResp> pageByChatRoomId(ChatRoomMessagePageAReq req) {
        return baseMapper.selectListByChatRoomId(req, (req.getPageNo() - 1) * req.getPageSize());
    }

    public int pageTotalByChatRoomId(ChatRoomMessagePageAReq req) {
        return baseMapper.selectListTotalByChatRoomId(req);
    }

    public void saveAndIncrement(SysChatRoomMessage data) {
        baseMapper.insertAndIncrement(data);
    }

    public void updateBeforeMessage(ChatRoomMessageSaveAReq req) {
        LambdaUpdateWrapper<SysChatRoomMessage> updateWrapper = WrapperUtil.instanceUpdate(SysChatRoomMessage.class)
                .set(SysChatRoomMessage::getBody, req.getMessageBody())
                .set(SysChatRoomMessage::getPublishTime, req.getPublishTime())
                .set(SysChatRoomMessage::getIsBefore, 0)
                .eq(SysChatRoomMessage::getId, req.getMessageId());
        super.update(updateWrapper);
    }
}
